package com.listeningframework.boot.codegen.constants;

import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.Date;

public enum JdbcTypeMapper {
    // @formatter:off
    VARCHAR("VARCHAR", String.class),
    INT("INT", Integer.class),
    INTEGER("INTEGER", Integer.class),
    BIGINT("BIGINT", Long.class),
    DECIMAL("DECIMAL", BigDecimal.class),
    DATETIME("DATETIME", Date.class),
    DATE("DATE", Date.class),
    TEXT("TEXT", String.class),
    LONGTEXT("LONGTEXT", String.class),
    TIMESTAMP("TIMESTAMP", Date.class);
    // @formatter:on

    private String code;
    private Class<?> clz;

    private JdbcTypeMapper(String code, Class<?> clz) {
        this.code = code;
        this.clz = clz;
    }

    /**
     * Getter method for property <tt>code</tt>.
     *
     * @return property value of code
     */
    public String getCode() {
        return code;
    }

    /**
     * Getter method for property <tt>clz</tt>.
     *
     * @return property value of clz
     */
    public Class<?> getClz() {
        return clz;
    }

    public static final Class<?> getType(String code) {
        String tempValue = "";
        if (StringUtils.isNotBlank(code)) {
            tempValue = StringUtils.upperCase(code);
        }
        JdbcTypeMapper[] mappers = values();
        Class<?> claz = null;
        for (JdbcTypeMapper mapper : mappers) {
            if (StringUtils.equals(mapper.getCode(), tempValue)) {
                claz = mapper.getClz();
                break;
            }
        }
        return claz;
    }
}
